home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / DOSSORT.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  3KB  |  114 lines

  1. /*
  2.  
  3.     cl /AL sortit.c
  4.  
  5. ===========================================================
  6.    sort.c       7-31-91  Robert Mashlan
  7.  
  8.    This filter is almost compatible with the MS-DOS filter of the same name.
  9.  
  10.    This filter sorts each line of standard input, disregarding case,
  11.    and sends it to standard output.
  12.  
  13.    optional parameters:  /R    Output in reverse order
  14.                          /+n   Compare at column n, 1 based
  15.  
  16.    example usage:  sort < unsorted.txt > sorted.txt
  17.  
  18.    note: compile in a far data model for maximum capacity ( compact or large )
  19.  
  20. */
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #define MAXLINES 10000        /* maximum number of lines to sort */
  27. #define MAXLINE  80        /* maximum line length */
  28.  
  29. int col     = 0;  /* column to start sort at ( zero based here ) */
  30. int reverse = 0;  /* reverse order flag      */
  31.  
  32. /*
  33. **  compare function for qsort
  34. */
  35.  
  36. int cmp( const void *a, const void *b)
  37. {
  38.       int result;
  39.       const char *_a = *(const char **)a;
  40.       const char *_b = *(const char **)b;
  41.  
  42.       /* compare at col if other than zero */
  43.  
  44.       if (col > 0)
  45.       {
  46.             if (strlen(_a) > col)
  47.                   _a += col;
  48.             else  _a = "";
  49.             if (strlen(_b) > col)
  50.                   _b += col;
  51.             else  _b = "";
  52.       }
  53.       result = stricmp(_a,_b);
  54.       return reverse ? -result : result;
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59.       static char *lines[MAXLINES];
  60.       int i, nlines=0, no_match;
  61.       char buf[MAXLINE];
  62.  
  63.       /* scan for command line options */
  64.  
  65.       for(i=1;i<argc;i++)
  66.       {
  67.             if(!stricmp(argv[i],"/R"))  /* reverse order option */
  68.                   reverse=1;
  69.             else if (!strnicmp(argv[i],"/+",2))  /* column number option */
  70.             {
  71.                   col=atoi(argv[i]+1)-1;
  72.                   if (col<0)
  73.                         col=0;
  74.             }
  75.             else
  76.             {
  77.                   fputs("Invalid Parameter",stderr);
  78.                   return 1;
  79.             }
  80.       }
  81.       while(1)
  82.       {
  83.             /* read the line */
  84.  
  85.             if(fgets(buf,MAXLINE,stdin)==NULL)
  86.                   break;
  87.  
  88.             /* Check for duplicates here */
  89.  
  90.             no_match = 1;
  91.             for ( i=0; i< nlines; i++ )
  92.                   if (!(no_match = memicmp(buf, lines[i], (unsigned int)12)))
  93.                         break;
  94.  
  95.             if (no_match)                 /* make a duplicate on the heap */
  96.             {
  97.                   if((lines[nlines++]=strdup(buf))==NULL)
  98.                   {
  99.                         fputs("SORT: Insufficent memory",stderr);
  100.                         return 2;
  101.                   }
  102.             }
  103.       }
  104.       /* sort the lot */
  105.       
  106.       qsort((void *)lines,nlines,sizeof(char *),cmp);
  107.  
  108.       /* display the sorted output */
  109.       
  110.       for (i=0;i<nlines;i++)
  111.             fputs(lines[i],stdout); /* send it to standard output */
  112.       return 0;
  113. }
  114.